package com.java.base.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLock {

    static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 5; i++) {
            int finalI = i;
            new Thread(() -> {
                try {
                    Thread.sleep(1000 * finalI);
                    readWriteLock.readLock().lock();
                    System.out.println(Thread.currentThread().getName() + ":可以读");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    readWriteLock.readLock().unlock();
                }
            }).start();
        }
        readWriteLock.writeLock().lock();
        System.out.println("加写锁");
        Thread.sleep(10000);
        System.out.println("释放写锁");
        readWriteLock.writeLock().unlock();

    }
}
